#!/usr/bin/env python2.5

## Packer for Similar Speech Tags

## Usage:  (modular)

import re, sys

def getTemplates(filename):
	"""Acquires the templates for our substitutions."""
	temps = dict()
	try:
		file = open(filename, 'r')
		for line in file:
			if (re.match('#', line)):	continue
			halves = line[:-1].split("\t")
			temps[halves[0]] = halves[-1] + " "
		file.close()
		return temps
	except:
		return None

def makeList(sentence):
	"""Converts a sentence from TOKEN/TAG form to list(TOKEN, TAG) form."""
	tokentag = list()
	for item in sentence.split():
		tt = item.split("/")
		tokentag.append(tuple([tt[0], tt[1]]))
	return tokentag

def unpackUs(sentlist):
	"""Unpacks a sentence (list of (token, tag) tuples) into two separate entities."""
	tag_str = token_str = ""
	for (token, tag) in sentlist:
		tag_str += tag + " "
		token_str += token + ","
	return (tag_str[:-1], token_str[:-1])

def lenOf(str, sep = " "):
	"""Determines the length of a string by counting intermittant separators."""
	if (not str):	return 0
	return (len(re.findall(sep, str.strip())) + 1)

def subIn(tag_str, token_str, start, end, pattern, filler):
	"""Substitutes a tag sequence with a catch-all label."""
	tag_str = re.sub(pattern, filler, tag_str, 1)
	tokens = token_str.split(',')
	# print tokens
	str = " ".join(tokens[start:end])
	# print str
	for y in range(start, end - 1):
		tokens.pop(start)
	tokens[start] = str
	token_str = ",".join(tokens)
	return (tag_str, token_str)

def findIn(tag_str, token_str, hole, peg):
	"""Finds the regex pattern in a string, replaces it with the peg, and returns the token and tag strings."""
	pattern = "(.*?)\\s?(%s)" % hole
	regex = r'(.*?)\s?(%s)' % hole
	# print regex
	find = re.match(regex, tag_str)
	while find:
		# print (find.group(1), find.group(2))
		st = lenOf(str(find.group(1)))
		nd = st + lenOf(str(find.group(2)))
		found = find.group(2)
		# print (tag_str, pattern, st, nd, found, peg)
		(tag_str, token_str) = subIn(tag_str, token_str, st, nd, found, peg)
		find = re.match(regex, tag_str)
	return (tag_str, token_str)

def subAll(tag_str, token_str, subDict):
	"""Performs all legal substitutions upon our token and tag strings."""
	for key in subDict.keys():
		(tag_str, token_str) = findIn(tag_str, token_str, key, subDict[key])
	return (tag_str, token_str)

def startUp(sentence, templates):
	"""Wrapper."""
	(tagstr, tokstr) = unpackUs(makeList(sentence))
	return (subAll(tagstr, tokstr, templates))

if (__name__ == "__main__"):
	## Main modeule.
	## Usage:  ~ templates.file sentence.file
	print
	file = sys.argv[1]
	senz = open(sys.argv[2], 'r')
	## Read in the templates.
	templates = getTemplates(file)
	if templates:
		for sentence in senz:
			print sentence
			print startUp(sentence, templates), "\n"
	else:
		## There was something wrong with the templates file, if it existed at all.
		print "You did not provide a legal filename for the templates."
	print
